//
//  DYTableViewCell.m
//  ZhangqiuForum
//
//  Created by 帝云科技 on 2018/1/17.
//  Copyright © 2018年 diyunkeji. All rights reserved.
//

#import "DYTableViewCell.h"

@interface DYTableViewCellItem ()

@property (nonatomic, weak, readwrite) Class cellClass;

@end

@implementation DYTableViewCellItem

- (NSString *)cellIdentifier {
    NSString *className = NSStringFromClass([self class]);
    if ([className hasSuffix:@"Item"]) {
        _cellIdentifier = [className substringToIndex:className.length - 4];
    }
    return _cellIdentifier;
}

- (CGFloat)cellHeight
{
    if (!_cellHeight)
    {
        _cellHeight = 0.f;
    }
    return _cellHeight;
}

- (Class)cellClass {
    return NSClassFromString(self.cellIdentifier);
}

+ (instancetype)item
{
    return [[[self class]alloc] init];
}

@end


@interface DYTableViewCell ()

@property (nonatomic, strong) UILabel *separatorLabel;

@end

@implementation DYTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self dy_initUI];
    }
    return self;
}

- (void)dy_initUI
{
    _sideMargin = 0;
    [self addSubview:self.separatorLabel];
    self.textLabel.textColor = [UIColor xhq_aTitle];
    self.textLabel.font = [UIFont xhq_font14];
    self.detailTextLabel.textColor = [UIColor xhq_content];
    self.detailTextLabel.font = [UIFont xhq_font14];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [_separatorLabel makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(0);
        make.height.equalTo(0.5f);
        make.leading.equalTo(_sideMargin);
        make.trailing.equalTo(-_sideMargin);
    }];
}

#pragma mark - setter
- (void)setItem:(DYTableViewCellItem *)item
{
    _item = item;
    if ([item isKindOfClass:[DYTableViewCellItem class]]) 
    {
        if ([NSString xhq_notEmpty:item.title])
        {
            self.textLabel.text = item.title;
        }
        if ([NSString xhq_notEmpty:item.imageName])
        {
            UIImage *image = [UIImage imageNamed:item.imageName];
            self.imageView.image = image;
        }
    }
}

- (void)setHideSeparatorLabel:(BOOL)hideSeparatorLabel
{
    if (_hideSeparatorLabel != hideSeparatorLabel)
    {
        _hideSeparatorLabel = hideSeparatorLabel;
        _separatorLabel.hidden = hideSeparatorLabel;
    }
}

- (void)setSideMargin:(CGFloat)sideMargin
{
    if (_sideMargin != sideMargin)
    {
        _sideMargin = sideMargin;
        [self layoutIfNeeded];
    }
}

#pragma mark - getter
- (UILabel *)separatorLabel
{
    if (!_separatorLabel)
    {
        _separatorLabel = [UILabel xhq_lineLabel];
    }
    return _separatorLabel;
}

@end
